package com.wenhai.spring.aop.features.advisor;

import com.wenhai.spring.aop.features.service.EchoService;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultIntroductionAdvisor;

/**
 * {@link org.springframework.aop.IntroductionAdvisor} 示例
 *
 * @author wenhai
 * @date   2021/5/2
 */
public class IntroductionAdvisorDemo implements EchoService,Comparable<IntroductionAdvisorDemo> {
    public static void main(String[] args) {
        IntroductionAdvisorDemo target = new IntroductionAdvisorDemo();
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(target);
        proxyFactory.addAdvisor(new DefaultIntroductionAdvisor((MethodBeforeAdvice) (method, args1, target1) ->
                System.out.println("BeforeAdvice: " + method),
                () -> new Class[]{EchoService.class}));
        Object proxy = proxyFactory.getProxy();
        EchoService echoService = (EchoService) proxy;
        echoService.echo("hello, world");
    }


    @Override
    public String echo(String message) {
        return "IntroductionAdvisorDemo: " + message;
    }

    @Override
    public int compareTo(IntroductionAdvisorDemo o) {
        return 0;
    }
}
